<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<meta http-equiv="Content-Type" content=
"text/html; charset=iso-8859-1">
<link rel='stylesheet' href='../rurple.css' type='text/css'>
<title>Fixing up Reeborg</title>
</head>
<body>

<h2 class="title">Fixing up Reeborg</h2>
<p>As we have seen, we can create new robots by using the notation</p>
<pre>
Reeborg = UsedRobot()
Erdna = UsedRobot(2, 2)
</pre>
<p>However, these robots are just like the original nameless one: they can only turn left.  To teach them how to turn right, we could define a function similar to what we did before:</p>
<pre>
<span class="keyword">def</span> Reeborg_turn_right():
    <span class="keyword">for</span> i <span class="keyword">in</span> range(3):
        Reeborg.turn_left()
</pre>
<p>However, there are at least two problems with this approach:</p>
<ul>
<li>We need to define a similar function for every single robot we create (Reeborg, Erdna, ...).</li>
<li><tt>Reeborg</tt> and <tt>turn</tt> are separated by an underscore
character "<tt>_</tt>" for the function we defined [to turn right] and by a dot "<tt>.</tt>" for the built-in method [to turn left].  This would not look right...</li>
</ul>
<p>A better approach is to define a new <span class="pykeyword">class</span> that would <i>inherit</i> from the class <tt>UsedRobot</tt>.  This is what we do in the next section.</p>

<h3 class="section">Designing a new <span class="pykeyword">class</span></h3>
<p>I will show you first how we can fix our robot so that it knows how to turn right, and explain what I did afterwords.</p>
<pre>
<span class="keyword">class</span> RepairedRobot(UsedRobot):
    <span class="keyword">def</span> turn_right(<span class="warning">synonym_used_to_refer_to_this_object</span>):
        <span class="keyword">for</span> i <span class="keyword">in</span> range(3):
            <span class="warning">synonym_used_to_refer_to_this_object</span>.turn_left()
</pre>
<p>Here's how we can then use this new <span class="pykeyword">class</span> of objects:</p>
<pre>
newReeborg = RepairedRobot()
newErdna = RepairedRobot(2, 2)

newReeborg.turn_left()    <span class="comment"># as before</span>
newReeborg.turn_right()   <span class="comment"># new method!</span>

newErdna.turn_right()     <span class="comment"># this one works too!</span>
</pre>
<p>And now, it is time to explain.  The Python keyword <span class="pykeyword">class</span> indicates that we are going to define a new type of "function", one that creates objects.  What follows <span class="pykeyword">class</span> is: <tt>RepairedRobot(UsedRobot)</tt>. <tt>RepairedRobot</tt> is the name of our new <span class="pykeyword">class</span>; by writing <tt>UsedRobot</tt> between the parentheses, we ensure that the new <span class="pykeyword">class</span>  <tt>RepairedRobot</tt> <i>inherits</i> all the methods and attributes that <tt>UsedRobot</tt> had.  Thus, when we write:</p>
<pre>
newReeborg = RepairedRobot()
</pre>
<p>we create a new robot "named" <tt>newReeborg</tt> which can do (at least all) the same things that the old</p>
<pre>
Reeborg = UsedRobot()
</pre>
<p>could do.</p>
<p>Next, inside the new <span class="pykeyword">class</span>, as indicated by the indented block, we define a new method, <tt>turn_right()</tt>.  By defining it inside the <span class="pykeyword">class</span>, we take the first step to insure that all the robots that are created by calling <tt>RepairedRobot()</tt> will be able to turn right!</p>
<p>The second step that is required is to tell Python that the method will "belong" to the particular object that has been created.  To do so, we use the variable <tt>synonym_used_to_refer_to_this_object</tt> which will refer to newReeborg, newErdna, etc., depending on what object is created.  When we write</p>
<pre>
newReeborg = RepairedRobot()
</pre>
<p>Python creates a new instance of the class <tt>RepairedRobot</tt> and defines all the methods, replacing the first argument of the method (<tt>synonym_used_to_refer_to_this_object</tt>) by the name of the instance (<tt>newReeborg</tt>).</p>
<p>Now, <tt>synonym_used_to_refer_to_this_object</tt> is rather a long name to type.  By convention, another variable name is used: <tt>self</tt>.  Thus, to follow the normal convention, I should have written:</p>
<pre>
<span class="keyword">class</span> RepairedRobot(UsedRobot):
    <span class="keyword">def</span> turn_right(self):
        <span class="keyword">for</span> i <span class="keyword">in</span> range(3):
            self.turn_left()
</pre>

<center><a href="36-tuples.htm"><img alt="previous" src=
"../../images/previous.png">Our last sequence</a> - <a href=
"../lessons_toc.htm"><img alt="home" src="../../images/home.png"></a> - <a href=
"38-class2.htm">A touch of class.<img alt="next"
src="../../images/next.png"></a></center>

</body>
</html>